package com.zhuss.timer;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.zhuss.util.HttpUtil;

/**
 * 自动重启 java 服务、有 app、admin、海康、转轴、snmp、jt809、es、 服务  每  8 分钟检测到服务未启动， 重启
 * @author Administrator
 *
 */

@Component
@Configuration  
@EnableScheduling
public class RestartJavaTask {
	
	private static Logger  LOGGER = LoggerFactory.getLogger(RestartJavaTask.class);
	
//	@Scheduled(cron = "0/10 * * * * ?")
	@Scheduled(cron = "0 0/8 * * * ?")
	private void configureTasks() {
		// wmic process where caption="java.exe" get processid,caption,commandline /value
		// wmic process where caption="java.exe" get commandline
		// wmic process where caption="javaw.exe" get processid,caption,commandline /value
		Boolean runRoll = isRunning("roll-1.0.0.jar");
		LOGGER.error("==================== roll ===" + runRoll);
		String cmdRoll ="D:\\hct\\jar\\roll\\start.bat";
		if (!runRoll) {
			restart(cmdRoll);
		}
		Boolean runHk = isRunning("kcnetsdk-0.0.1-SNAPSHOT.jar");
		LOGGER.error("====================  hk ===" + runHk);
		String cmdHk ="D:\\hct\\jar\\hcnetdomo\\checkMonitorJar\\start.bat";
		if (!runHk) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			restart(cmdHk);
		}
		Boolean runJt809 = isRunning("jt809-0.0.1-SNAPSHOT.jar");
		LOGGER.error("==================== jt809 ===" + runJt809);
		String cmdJt809 ="D:\\hct\\jar\\jt809\\start.bat";
		if (!runJt809) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			restart(cmdJt809);
			
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			HttpUtil.goPageByBrowse("http://127.0.0.1:9100/socket/start");
			
		}
		Boolean runES = isRunning("Delasticsearch -Des.path.home=\"D:\\hct\\ELK\\ELK\\ES\\elasticsearch-6.8.1\"");
		LOGGER.error("====================  es ===" + runES);
		String cmdES ="D:\\hct\\ELK\\ELK\\ES\\elasticsearch-6.8.1\\bin\\elasticsearch.bat";
		if (!runES) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			restart(cmdES);
			
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			HttpUtil.goPageByBrowse("http://127.0.0.1:9200/");
			
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			HttpUtil.goPageByBrowse("chrome-extension://ffmkiejjmecolpfloofpjologoblkegm/elasticsearch-head/index.html");
		}
		
		Boolean runSnmp = isRunning("snmp-0.0.1-SNAPSHOT.jar");
		LOGGER.error("==================== snmp ===" + runSnmp);
		String cmdSnmp ="D:\\hct\\jar\\snmp\\start.bat";
		if (!runSnmp) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			restart(cmdSnmp);
		}
		Boolean runLog = isRunning("\"D:\\hct\\ELK\\ELK\\LG\\logstash-7.2.0\\logstash-core\\lib\\jars\\guava-22.0.jar\"");
		LOGGER.error("==================== log ===" + runLog);
		String cmdLog ="D:\\hct\\jar\\bat\\logstash.bat";
		if (!runLog) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			restart(cmdLog);
		}
		Boolean runApp = isRunning("zhhct-app.jar");
		LOGGER.error("==================== app ===" + runApp);
		String cmdApp ="D:\\hct\\jar\\hct-app\\start.bat";
		if (!runApp) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			restart(cmdApp);
		}
		Boolean runAdmin = isRunning("zhhct-admin.jar");
		LOGGER.error("==================== admin  ===" + runAdmin);
		String cmdAdmin ="D:\\hct\\jar\\hct-admin\\start.bat";
		if (!runAdmin) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			restart(cmdAdmin);
		}
		// System.err.println("RestartSocketTask-------执行静态定时任务时间: " + LocalDateTime.now());
	}
	
	public static void main(String[] args) {
	}
	
	public void restart(String cmd) {
//		Process obj = null;
         try {
        	Thread.sleep(1000);
            Runtime.getRuntime().exec(cmd);
            //int status = obj.waitFor();
            //LOGGER.error("status: " + status);
            // LOGGER.error(JsonUtil.toJson(obj));
         } catch (Exception e) {
        	e.printStackTrace();
        	//LOGGER.error(JsonUtil.toJson(obj));
         }
	}


	public static boolean isRunning(String jarName) {
		BufferedReader bufferedReader = null;
		try {
			Process proc = Runtime.getRuntime().exec("wmic process where caption=\"java.exe\" get commandline");
			bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				// LOGGER.info("===============" + ++i + ": " + line);
				if (line.contains(jarName)) { //判断是否存在
					return true;
				}
			}
			return false;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (Exception ex) {
					
				}
			}
		}
	}
}
